package com.turman.plyopengl.objects

import android.opengl.GLES20
import com.turman.plyopengl.data.Constants
import com.turman.plyopengl.data.VertexArray
import com.turman.plyopengl.getVertexArray
import com.turman.plyopengl.programs.PlyShaderProgram
import com.turman.plyopengl.vertexCount
import java.io.InputStream

class PlyObject(private val inputStream: InputStream) {
    companion object {
        private const val POSITION_COMPONENT_COUNT = 3
        private const val COLOR_COMPONENT_COUNT = 3
        private const val STRIDE: Int = (POSITION_COMPONENT_COUNT
                + COLOR_COMPONENT_COUNT) * Constants.BYTE_PER_FLOAT
    }

    private var vertexArray: VertexArray?=null

    fun bindData(plyShaderProgram: PlyShaderProgram) {
        if (vertexArray == null) {
            //加载点数据
            vertexArray = getVertexArray(inputStream)
        }
        vertexArray?.setVertexAttribPointer(
            dataOffset = 0,
            attributeLocation = plyShaderProgram.aPositionLocation,
            componentCount = POSITION_COMPONENT_COUNT,
            stride = STRIDE
        )
        vertexArray?.setVertexAttribPointer(
            3,
            attributeLocation = plyShaderProgram.aColorLocation,
            componentCount = COLOR_COMPONENT_COUNT,
            stride = STRIDE
        )
    }



    fun draw() {
        GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vertexCount.toInt())
    }
}
















